package m.jclecai.com.fragment;

import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.adapter.HotTrendAdapter;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.bean.TrendButtonBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.MyToast;
import m.jclecai.com.utils.ScreenUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.utils.http.SystemUtils;
import m.jclecai.com.utils.http.responseBody.BaseBody;

/**
 * Created by ZD on 2017/4/26.
 * 冷热走势
 */

public class HotTrendFragment extends BaseFragment {

    @BindView(R.id.rg_num)
    RadioGroup rgNum;
    @BindView(R.id.lvBody)
    ListView lvBody;

    //彩票种类id
    String lotteryid = "19";
    //期数
    String issuecount = "30";
    //所选按钮参数
    String idx = "num_info1";
    HotTrendAdapter adapter;
    JsonArray datas;
    TrendButtonBean bean;
    @Override
    public int getLayoutId() {
        return R.layout.fragment_trend_hot;
    }

    @Override
    public void initView() {
        datas = new JsonArray();
        addRadioButton(bean);
        adapter = new HotTrendAdapter(getActivity(),datas);
        lvBody.addHeaderView(View.inflate(getActivity(),R.layout.view_trend_list_header,null));
        lvBody.setAdapter(adapter);
    }

    public void setTrendData(TrendButtonBean data){
        bean = data;
    }

    /**
     * 动态添加按钮
     *
     * @param bean
     */
    void addRadioButton(TrendButtonBean bean) {
        int width = ScreenUtil.getScreenWidth(getActivity());
        for (int i = 0; i < bean.getHnames().length; i++) {
            lotteryid = String.valueOf(bean.getLotteryid());
            RadioButton rb = new RadioButton(getActivity());

            rgNum.addView(rb, new LinearLayout.LayoutParams(width / bean.getHnames().length, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
            rb.setCompoundDrawablesWithIntrinsicBounds(null, null, null, ContextCompat.getDrawable(getActivity(), R.drawable.tab_radio_driver));
            rb.setButtonDrawable(R.color.transparent);
            rb.setTextColor(ContextCompat.getColor(getActivity(), R.color.color_333));
            rb.setGravity(Gravity.CENTER);
            rb.setText(bean.getHnames()[i]);
            String param = new Gson().toJson(bean) + "_" + i;
            rb.setTag(param);
            rb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String s = v.getTag().toString();
                    String json = s.substring(0, s.lastIndexOf("_"));
                    String id = s.substring(s.lastIndexOf("_") + 1);
                    int index = Integer.parseInt(id);
                    TrendButtonBean b = new Gson().fromJson(json, TrendButtonBean.class);
                    idx = b.getHidxs()[index];
                    loadData();
                }
            });
            if(i==0){
                rgNum.check(rb.getId());
                idx = bean.getHidxs()[i];
                loadData();
            }
        }
    }

    @Override
    public void loadData(String issuecount){
        super.loadData(issuecount);
        this.issuecount = issuecount;
        loadData();
    }

    //加载数据
    private void loadData() {
        showDialog();
        Map<String,String> map = new HashMap<>();
        map.put("lotteryid",lotteryid);
        map.put("issuecount",issuecount);
        map.put("idx",idx);
        HttpManager.postAsyn(Constant_Url.HOT_CODE, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                MyToast.showToast(getActivity(),getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                BaseBody baseBody = FastOk.get(response, BaseBody.class);
                if(baseBody != null && baseBody.getStatus() == 0) {
                    response = SystemUtils.jsonp2JsonString(response);
                    JsonObject jb = new Gson().fromJson(response, JsonObject.class);
                    if (jb.get("placenum") == null) return;
                    datas = jb.get("placenum").getAsJsonArray();
                    adapter.updateNotifyDataSetChanged(datas);
                }else {
                    MyToast.showToast(getActivity(),getString(R.string.error_network));
                }
            }
        },map);
    }
}
